package com.qf.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.qf.dao.OrderSettingDao;
import com.qf.pojo.OrderSetting;
import com.qf.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author zhouhao
 * @create 2021-12-23 16:45
 */
@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    //通过dubbo远程调用服务实现数据批量导入到数据库
    @Override
    public void add(ArrayList<OrderSetting> data) {
        if (data != null && data.size() > 0){
            for (OrderSetting orderSetting : data){
                //判断当前日期是否已经进行了预约设置
                long count =  orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
                if (count > 0){
                    //已有预约设置
                    orderSettingDao.updateNumber(orderSetting);
                }else {
                    //添加预约设置
                    orderSettingDao.add(orderSetting);
                }
            }
        }
    }

    //根据月份查询对应的预约设置数据
    @Override
    public List<Map<String, Object>> getOrderSettingByMonth(String date) { //date格式 2021-12
        //设置条件范围
        String begin = date + "-1";
        String end = date + "-31";
        HashMap<String, String> condition = new HashMap<>();
        condition.put("begin",begin);
        condition.put("end",end);
        List<OrderSetting> list = orderSettingDao.getOrderSettingByMonth(condition);
        List<Map<String, Object>> result = new ArrayList<>();
        for (OrderSetting orderSetting : list){
            HashMap<String, Object> map = new HashMap<>();
            map.put("date",orderSetting.getOrderDate().getDate());//获取日期的记号
            map.put("number",orderSetting.getNumber());
            map.put("reservations",orderSetting.getReservations());
            result.add(map);
        }
        return result;
    }

    //设置预约人数
    @Override
    public void setNumber(OrderSetting orderSetting) {
        //查看当天是否有预约设置
        Date orderDate = orderSetting.getOrderDate();
        //判断当前日期是否已经进行了预约设置
        long count =  orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
        if (count > 0){
            //已有预约设置
            orderSettingDao.updateNumber(orderSetting);
        }else {
            //添加预约设置
            orderSettingDao.add(orderSetting);
        }
    }
}
